﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace Toolkit
{
    public class TimeRibbon : Control
    {
        public TimeRibbon()
        {
            this.DefaultStyleKey = typeof(TimeRibbon);
        }



        public DateTime Origin
        {
            get { return (DateTime)GetValue(OriginProperty); }
            set { SetValue(OriginProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Origin.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OriginProperty =
            DependencyProperty.Register("Origin", typeof(DateTime), typeof(TimeRibbon), new PropertyMetadata(DateTime.Now));



        public TimeSpan TimeSpanPerPixel
        {
            get { return (TimeSpan)GetValue(TimeSpanPerPixelProperty); }
            set { SetValue(TimeSpanPerPixelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TimeSpanPerPixel.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TimeSpanPerPixelProperty =
            DependencyProperty.Register("TimeSpanPerPixel", typeof(TimeSpan), typeof(TimeRibbon), new PropertyMetadata(TimeSpan.FromTicks(0)));


        private StackPanel layoutRoot;
        private StackPanel stackPanelDragable;
        public override void OnApplyTemplate()
        {
            this.layoutRoot = this.GetTemplateChild("LayoutRoot") as StackPanel;
            Debug.Assert(this.layoutRoot != null, "LayoutRoot is null");
            Canvas canvas = this.layoutRoot.Children.ElementAt(0) as Canvas;
            stackPanelDragable  = canvas.Children.ElementAt(2) as StackPanel;

            base.OnApplyTemplate();
        }

        private Point? leftButtonDown;
        private Point? leftButtonUp; 
        private object sync = new object();
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            e.Handled = true;
            lock (sync)
            {
                Point pos = e.GetPosition(layoutRoot);
                leftButtonDown = new Point(pos.X - Margin.Left, pos.Y - Margin.Top);
                CaptureMouse();
            }
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            e.Handled = true;
            lock (sync)
            {
                leftButtonUp = e.GetPosition(layoutRoot);
                //currentMargin = new Thickness(currentPosition.X - leftButtonDown.Value.X, currentPosition.Y - leftButtonDown.Value.Y, 0, 0);
                leftButtonDown = null;
                ReleaseMouseCapture();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            lock (sync)
            {
                if (leftButtonDown.HasValue)
                {
                    Point currentPosition = e.GetPosition(layoutRoot);
                    double upX = 0.0;
                    double upY = 0.0;
                    if (leftButtonUp.HasValue)
                    {
                        upX = leftButtonUp.Value.X;
                        upY = leftButtonUp.Value.Y;
                        //dleft = currentMargin.Value.Left;
                        //dtop = currentMargin.Value.Top;
                    }
                    stackPanelDragable.Margin = new Thickness(currentPosition.X - leftButtonDown.Value.X + upX, currentPosition.Y - leftButtonDown.Value.Y + upY, 0, 0);
                }
            }   
        }

    }
}
